package zookeeper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

public class SyncTest2 {

	private static final Log log = LogFactory.getLog(SyncTest2.class);
	
	public static final String watcher = "/watcher";
	
	public static void main(String[] args){
		ZooKeeper zk = ZooKeeperUtil.initZooKeeper();
		try {
			zk.create(watcher, new byte[0],ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
			zk.exists(watcher, new Watcher() {
				
				@Override
				public void process(WatchedEvent event) {
					log.info(event.getPath()+" "+ event.getType()+" "+event.getState());
				}
			});
		} catch (KeeperException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			Thread.sleep(5*1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			zk.create(SyncTest2.watcher+"/demo3", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
		} catch (KeeperException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
